package cn.kgc.shiroday03.exception;

import cn.kgc.shiroday03.commons.CodeMsg;
import cn.kgc.shiroday03.commons.ResponseResult;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/6/5
 * @Description:全局异常处理类
 */
@RestControllerAdvice
public class GlobalExceptionHandler {


    // 用户名错误
    @ExceptionHandler(UnknownAccountException.class)
    public ResponseResult unknownAccountExceptionHandler(UnknownAccountException e){
           return  new ResponseResult(CodeMsg.USER_NOT_EXIST);
    }

    // 密码错误
    @ExceptionHandler(IncorrectCredentialsException.class)
    public ResponseResult incorrectCredentialsExceptionHandler(IncorrectCredentialsException e){
        return  new ResponseResult(CodeMsg.CREDENTIAL_ERROR);
    }


    //  账户被禁用  LockedAccountException
    @ExceptionHandler(LockedAccountException.class)
    public ResponseResult lockedAccountExceptionHandler(LockedAccountException e){
        return  new ResponseResult(CodeMsg.USER_LOCKED_ERROR);
    }



    @ExceptionHandler(AuthorizationException.class)
    public ResponseResult  authorizationExceptionHandler(AuthorizationException e){
        return  new ResponseResult(CodeMsg.NO_AUTHORIZATION);
    }

    @ExceptionHandler(Exception.class)
    public ResponseResult otherExceptionHandler(Exception e){
        e.printStackTrace();
        return  new ResponseResult(CodeMsg.ERROR);
    }

}
